<template>
	<view class="page" :style="{paddingTop: `${topHeight}px`}">
		<!--头部-->
		<view class="header">
			<view class="h-l">
				<up-icon color="#000" name="arrow-left" size="22" @tap="goBack"></up-icon>
			</view>
			<view class="h-c">
				<text>看日志</text>
			</view>
			<view class="h-r">
				
			</view>
		</view>
		<view class="content">
			<view class="search">
				<view class="item">
					<text>按模板筛选</text>
					<view class="templete">
						<view class="card" v-for="(item, index) in templetes" :key="index" @click="handleClick(index)">
							<view :class="[templeteIndex == index?'tag-active':'tag']">
								<text>{{item.text}}</text>
							</view>
						</view>
						<view class="card">
							<view class="tag">
								<view style="display: flex;flex-direction: row;justify-content: center;">
									<uni-icons type="down" size="20" color="#909399"></uni-icons>
									<text style="margin-left: 5rpx;color: #909399;">更多模板</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="item">
					<text>按发送人筛选</text>
					<view class="person">
						<!--人员列表-->
						<view class="list" v-for="(p, index) in personList" :key="index">
							<view class="avatar">
								<view>
									<image style="width: 66rpx;height: 66rpx;border-radius: 5rpx;" :src="p.avatar"></image>
								</view>
								<text style="color: #909399;">{{p.name}}</text>
								<view class="close-btn" @click="delPerson(index)">
									<uni-icons type="close" size="18" color="#909399"></uni-icons>
								</view>
							</view>
						</view>
						
						<view class="list">
							<view class="avatar" style="border:1px solid #f2f1f6;border-radius: 5rpx;padding-bottom: 10rpx;" @click="selectPerson">
								<view style="line-height: 66rpx;">
									<uni-icons type="plusempty" size="20" color="#909399"></uni-icons>
								</view>
								<text>添加</text>
							</view>
						</view>
					</view>
				</view>
				<view class="item">
					<up-cell-group>
						<up-cell
							title="开始时间"
							:value="form.start_date"
							:isLink="true"
							@click="startDateShow = true"
						></up-cell>
						<up-cell
							title="结束时间"
							:value="form.end_date"
							:isLink="true"
							@click="endDateShow = true"
						></up-cell>
					</up-cell-group>
					<!--开始时间-->
					<up-calendar :show="startDateShow" @close="startDateShow = false" 
						@confirm="handleStartDate" :show-confirm="false"
						:minDate="form.minDate" :maxDate="form.maxDate"
						:monthNum="12"
						:closeOnClickOverlay="true">
					</up-calendar>
					<!--结束时间-->
					<up-calendar :show="endDateShow" @close="endDateShow = false" 
						@confirm="handleEndDate" :show-confirm="false"
						:minDate="form.minDate" :maxDate="form.maxDate"
						:monthNum="12"
						:closeOnClickOverlay="true">
					</up-calendar>
				</view>
			</view>
			<view class="btn">
				<up-button text="取消" @tap="goBack" shape="circle"></up-button>
				<up-button type="primary" text="确定" @tap="confirmBack" shape="circle"></up-button>
			</view>
		</view>
	</view>
</template>

<script>
	import dayjs from "dayjs"
	import { getCurrentInstance} from "vue";
	var _this;
	export default {
		name:'SearchLog',
		data() {
			return {
				topHeight: 0,
				templetes: [
					{text:'日报',checked:false},
					{text:'周报',checked:false},
					{text:'月报',checked:false}
				],
				templeteIndex:null,
				templeteName:null,
				personList:[],
				form: {
					start_date:'请选择',
					end_date:'请选择',
					minDate:dayjs().startOf('year').format('YYYY-MM-DD'),
					maxDate:dayjs().format('YYYY-MM-DD'),
				},
				startDateShow:false,
				endDateShow:false,
				selectTab:0,
			}
		},
		created(){
			uni.$off('WorklogAddUser');
			_this = this;
			let app = uni.getSystemInfoSync();
			this.topHeight = app.statusBarHeight;
			
			//监听选择人员信息
			uni.$on('WorklogAddUser',(data)=>{
				if(_this.personList && _this.personList.length > 0) {
					data.forEach(p=>{
						_this.personList.push(p);
					});
					//去重
					const seen = new Map();
					_this.personList = _this.personList.filter(item=>{
						return !seen.has(item.id) && seen.set(item.id, true);
					});
				}else {
					_this.personList = data;
				}
			});
			
			const instance = getCurrentInstance().proxy;
			const eventChannel = instance.getOpenerEventChannel();
			eventChannel.on('logSearch', async function(data) {
				_this.selectTab = data.selectTab;
			});
		},
		methods: {
			goBack () {
				uni.navigateBack({ delta: 1 });
			},
			handleClick(index) {
				this.templeteIndex = index;
				this.templeteName = this.templetes[index].text;
			},
			selectPerson() {
				uni.navigateTo({
					url: '/components/OrgPicker/index?overlay=overlay&entry=component&VisibleandHidden=true&title=人员选择&source=WorklogAddUser'
				});
			},
			delPerson(index) {
				_this.personList.splice(index, 1);
			},
			handleStartDate(date) {
				this.form.start_date = date[0];
				this.startDateShow = false;
			},
			handleEndDate(date) {
				this.form.end_date = date[0];
				this.endDateShow = false;
			},
			confirmBack() {
				let idArr = [];
				_this.personList.forEach(p=>{
					idArr.push(p.id);
				});
				let params = { 
					templete_name: _this.templeteName,
					add_id: idArr.join(','),
					start_date: _this.form.start_date=='请选择'?'':_this.form.start_date,
					end_date: _this.form.end_date=='请选择'?'':_this.form.end_date,
					select_tab: _this.selectTab,
				}
				uni.reLaunch({
					url:'/pages/worklog/index?searchForm='+JSON.stringify(params)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		padding-bottom: 50rpx;
		overflow: hidden;
	}
	.header {
		position: relative;
		z-index: 9999;
		padding: 10rpx 20rpx;
		display: flex;
		align-items: center;
		.h-l {
			width: 30%;
			display: flex;
			flex-direction: row;
			gap: 20rpx;
		}
		.h-c {
			width: 40%;
			text-align: center;
		}
		.h-r {
			width: 30%;
			display: flex;
			justify-content: flex-end;
		}
	}
	.content {
		display: flex;
		flex-direction: column;
		height: 91vh;
		.search {
			flex: 1 1 auto;
			background-color: #f1f2f6;
			margin-bottom: 20rpx;
			padding: 20rpx;
			.item {
				background-color: #fff;
				border-radius: 30rpx;
				padding: 15rpx;
				margin-bottom: 20rpx;
				.templete {
					display: flex;
					flex-wrap: wrap;
					font-size: 28rpx;
					margin-top: 20rpx;
					.card {
						width: 50%;
						height: 0;
						padding-bottom: 15%;
						text-align: center;
						.tag {
							border: 1px solid #f2f1f6;
							margin-right: 20rpx;
							border-radius: 30rpx;
							padding: 15rpx 0;
						}
						.tag-active {
							border: 1px solid #3f85ff;
							margin-right: 20rpx;
							border-radius: 30rpx;
							padding: 15rpx 0;
							color: #3f85ff;
							background-color: #e0edff;
						}
					}
				}
				.person {
					display: flex;
					flex-wrap: wrap;
					gap: 20rpx;
					font-size: 26rpx;
					margin-top: 20rpx;
					.list {
						width: 16%;
						height: 0;
						padding-bottom: 16%;
						box-sizing: border-box;
						.avatar {
							display: flex;
							flex-direction: column;
							justify-content: center;
							text-align: center;
							position: relative;
							.close-btn {
								position: absolute;
								top: -8px;
								right: -1px;
								z-index: 999;
							}
						}
					}
				}
			}
		}
		.btn {
			flex: 0 0 auto;
			display: flex;
			flex-direction: row;
			gap: 20rpx;
			padding: 0 20rpx;
		}
	}
</style>
